<?php


namespace servers;


class SmsServer
{
    public  function sendSms($phone,$debug = true)
    {
        if($debug){
            $code = mt_rand(1000,9999);
            self::validateCode($phone,$code);
            return ['status=0','data'=>$code,'发送成功'];

        }

        $statusStr = [
            "0" => "短信发送成功",
            "-1" => "参数不全",
            "-2" => "服务器空间不支持,请确认支持curl或者fsocket，联系您的空间商解决或者更换空间！",
            "30" => "密码错误",
            "40" => "账号不存在",
            "41" => "余额不足",
            "42" => "帐户已过期",
            "43" => "IP地址限制",
            "50" => "内容含有敏感词"
        ];
        $smsapi = "http://api.smsbao.com/";
        $user = "cyf_859900"; //短信平台帐号
        $pass = '9f2ce0eb76f246669ca90046fd3cccf0'; //短信平台密码
        $sendurl = $smsapi."sms?u=".$user."&p=".$pass."&m=".$phone."&c=".urlencode($content);
        $result =file_get_contents($sendurl) ;
        echo $statusStr[$result];
    }

    /**
     * 缓存验证码
     * @param $phone
     * @param $code
     * @return array
     */
    public static function validateCode($phone,$code)
    {
        //定义缓存
        $cache = \Yii::$app->cache;
        //缓存发送验证码
        $sendSmsCode = 'smscode'.$phone;
        //缓存发送验证码时间
        $sendTime = 'sendTime'.$phone;
        //缓存发送验证码次数
        $sendCount = 'sendCount'.$phone;

        $nowTime = time();//获取当前时间
        $getSendTime = $cache->get($sendTime);//得到发送验证码时间

        //缓存验证码
        $cache->set($sendSmsCode,$code,600);
        //缓存发送时间
        $cache->set($sendTime,time(),60);

        return true;

    }
}